package com.lx.dto;

import com.lx.entity.User;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 用于Security登录安全用户
 *
 * @author 段誉
 * @create 2019-05-21 8:56
 */
public class SecurityUser extends User implements UserDetails {
  private static final long serialVersionUID = -5508674219766617363L;

  public SecurityUser(User user) {
    if (user != null) {
      this.setId(user.getId());
      this.setName(user.getName());
      this.setAge(user.getAge());
      this.setUsername(user.getUsername());
      this.setPassword(user.getPassword());
    }
  }

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    Collection<GrantedAuthority> authorities = new ArrayList<>();
    String username = this.getUsername();
    if (username != null) {
      SimpleGrantedAuthority authority = new SimpleGrantedAuthority(username);
      authorities.add(authority);
    }
    return authorities;
  }

  /**
   * 账户是否过期，过期无法验证
   * @return
   */
  @Override
  public boolean isAccountNonExpired() {
    return true;
  }

  /**
   * 指定用户是否解锁，锁定的用户无法进行身份验证
   * @return
   */
  @Override
  public boolean isAccountNonLocked() {
    return true;
  }

  /**
   * 指示是否已过期的用户的凭证（密码），过期的凭证防止认证
   * @return
   */
  @Override
  public boolean isCredentialsNonExpired() {
    return true;
  }

  /**
   * 是否可用，禁用的用户不能身份验证
   * @return
   */
  @Override
  public boolean isEnabled() {
    return true;
  }
}
